package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.SysUserDAO;
import com.foreveross.vo.SysRole;
import com.foreveross.vo.SysUser;

@Component("userDAO")
public class SysUserDAOImp extends SQLUtil implements SysUserDAO {

	@Override
	public List<SysUser> findUserList(String name, String account,
			int pageIndex, int pageSize) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select model from SysUser model where 1=1 ");
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(account) && null != account) {
			queryString.append(" and model.account like '%" + account + "%'");
		}
		queryString.append(" order by model.createTime desc");
		return (List<SysUser>) findObjectAll(queryString.toString(), pageIndex,
				pageSize);
	}

	@Override
	public long getUserListCount(String name, String account) throws Exception {
		StringBuffer queryString = new StringBuffer(
				"select count(*) from SysUser model where 1=1 ");
		if (!"".equals(name) && null != name) {
			queryString.append(" and model.name like '%" + name + "%'");
		}
		if (!"".equals(account) && null != account) {
			queryString.append(" and model.account like '%" + account + "%'");
		}
		return (Long) findObjectAll(queryString.toString()).get(0);
	}

	@Override
	public void deleteUser(String userId) throws Exception {
		final String sqlv = "delete from SysUserRole t where t.sysUser.id='"
				+ userId + "'";
		updateObejctByHql(sqlv);
		final String sql = "delete from SysUser t where t.id='" + userId + "'";
		updateObejctByHql(sql);
	}

	@Override
	public void deleteUserRole(String userid) throws Exception {
		final String sql = "delete from SysUserRole ur  where ur.sysUser.id='"
				+ userid + "'";
		updateObejctByHql(sql);
	}

	@Override
	public void deleteRoleMemu(String roleid) throws Exception {
		final String sql = "delete from SysRoleMenu ur  where ur.sysRole.id='"
				+ roleid + "'";
		updateObejctByHql(sql);
	}

	@Override
	public SysUser login(String account) throws Exception {
		final String sql = "select user from SysUser user where user.account='"
				+ account + "'";
		return (SysUser) findObejctByHql(sql);
	}

	@Override
	public String getUserRole(String userId) throws Exception {
		String sql = "select r.* from sys_role r, (SELECT m.* FROM sys_user_role m WHERE m.sys_user_id='"
				+ userId + "') t  where r.id=t.sys_role_id";
		List<SysRole> list = (List<SysRole>) findObejctBySQL(sql, SysRole.class);
		StringBuffer str = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			SysRole r = list.get(i);
			str.append("'").append(r.getId()).append("'").append(",");
		}
		if (str.length() > 1)
			return str.substring(0, str.length() - 1).toString();
		return "";
	}
	
	public List<SysRole> getUserRoleList(String userId) throws Exception {
		String sql = "select r.* from sys_role r, (SELECT m.* FROM sys_user_role m WHERE m.sys_user_id='"
				+ userId + "') t  where r.id=t.sys_role_id";
		return (List<SysRole>) findObejctBySQL(sql, SysRole.class);		
	}

	@Override
	public Boolean updateUserPass(String userId, String pass) throws Exception {
		SysUser user = (SysUser) findById(SysUser.class, userId);
		if (user != null) {
			String sql = "update sys_user set password='" + pass
					+ "' where id='" + userId + "'";
			updateObejctBySql(sql);
			return true;
		} else
			return false;
	}

	@Override
	public void deleteRoleHouse(String roleid) throws Exception {
		final String sql = "delete from SysRoleHouse uh  where uh.sysRole.id='"
				+ roleid + "'";
		updateObejctByHql(sql);
	}

}
